import apiRequest from '@/api/request'

// 获取角色列表
export const getRoleListApi = (params?: any) => {
  return apiRequest.get('/role/list', { params }).then(res => Array.isArray(res) ? res : (res.data || res))
}

// 根据ID获取角色
export const getRoleByIdApi = (id: number) => {
  return apiRequest.get(`/role/${id}`).then(res => res.data || res)
}

// 创建角色
export const createRoleApi = (data: any) => {
  return apiRequest.post('/role/create', data).then(res => res.data || res)
}

// 更新角色
export const updateRoleApi = (data: any) => {
  return apiRequest.post('/role/update', data).then(res => res.data || res)
}

// 删除角色
export const deleteRoleApi = (id: number) => {
  return apiRequest.post(`/role/delete/${id}`).then(res => res.data || res)
}

// 获取角色的菜单权限
export const getRoleMenusApi = (id: number) => {
  return apiRequest.get(`/role/${id}/menus`).then(res => Array.isArray(res) ? res : (res.data || res))
}

// 分配角色菜单权限
export const assignRoleMenusApi = (id: number, menuIds: number[]) => {
  return apiRequest.post(`/role/${id}/menus`, { menuIds }).then(res => res.data || res)
}